﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMPLib;

namespace demo04b
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        #region "Biến toàn cục"
        IWMPPlaylistCollection playlistcol;
        IWMPPlaylistArray playlistarray;
        IWMPPlaylist playlist;
        IWMPMediaCollection mediacol;
        int iCount;
        int curr = 0;
        int titleindex, artistindex, albumindex, ratingindex, yearindex;
        #endregion

        #region "Get Media Info"
        string getYear(IWMPMedia media)
        {
            string year = "";
            yearindex = mediacol.getMediaAtom("ReleaseDate");
            year = media.getItemInfoByAtom(yearindex);
            if (string.IsNullOrEmpty(year))
            {
                year = "Unknown Year";
            }
            else
            {
                string[] tmp = year.Split(' ');
                string[] tmp2 = tmp[0].Split('-');
                year = tmp2[2];
            }
            return year;
        }

        string getTitle(IWMPMedia media)
        {
            string title = "";
            titleindex = mediacol.getMediaAtom("Title");
            title = media.getItemInfoByAtom(titleindex);
            if (string.IsNullOrEmpty(title))
            {
                title = media.name;
            }
            return title;
        }

        string getArtist(IWMPMedia media)
        {
            string artist = "";
            //artist = media.getItemInfo("AlbumArtist");
            artistindex = mediacol.getMediaAtom("AlbumArtist");
            artist = media.getItemInfoByAtom(artistindex);
            if (string.IsNullOrEmpty(artist))
            {
                artist = "Unknown artist";
            }
            return artist;
        }

        string getAlbum(IWMPMedia media)
        {
            string album = "";
            albumindex = mediacol.getMediaAtom("Album");
            album = media.getItemInfoByAtom(albumindex);
            if (string.IsNullOrEmpty(album))
            {
                album = "Unknown Album";
            }
            return album;
        }

       
        string getRating(IWMPMedia media)
        {
            string rating = "";
            ratingindex = mediacol.getMediaAtom("UserRating");
            rating = media.getItemInfoByAtom(ratingindex);
            int tmp = int.Parse(rating);
            switch(tmp)
            {
                case 1:
                    rating = "1";
                    break;
                case 25:
                    rating = "2";
                    break;
                case 50:
                    rating = "3";
                    break;
                case 75:
                    rating = "4";
                    break;
                case 99:
                    rating = "5";
                    break;
                default:
                    rating = "0";
                    break;
            }
            return rating;
        }


        void GetInfo(IWMPMedia media)
        {
            lblTitle.Text = getTitle(media);
            lblSinger.Text = getArtist(media);
            lblAlbum.Text = getAlbum(media);
            lblRating.Text = getRating(media);
            lblYear.Text = getYear(media);
        }
        #endregion

        #region "Lấy danh sách playlist"
        public void ShowPlaylist()
        {
            playlistcol = axWindowsMediaPlayer1.playlistCollection;
            playlistarray = playlistcol.getAll();
            iCount = playlistarray.count;
            comboBox1.BeginUpdate();
            for (int i = 0; i < iCount; i++)
            {
                comboBox1.Items.Add(playlistarray.Item(i).name);
            }
            comboBox1.EndUpdate();
            comboBox1.SelectedIndex = 0;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ShowPlaylist();
        }
        #endregion
        
        #region "Load danh sách bài nhạc"
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            playlist = playlistarray.Item(comboBox1.SelectedIndex);
            string ItemNames = "";
            listBox1.BeginUpdate();
            for (int j = 0; j < playlist.count; j++)
            {
                ItemNames = playlist.get_Item(j).name;
                listBox1.Items.Add(ItemNames);
            }
            listBox1.EndUpdate();
            curr = 0;
            listBox1.SelectedIndex = curr;
            axWindowsMediaPlayer1.currentPlaylist = playlist;
            axWindowsMediaPlayer1.Ctlcontrols.play();
            mediacol = axWindowsMediaPlayer1.mediaCollection;
            IWMPMedia item = axWindowsMediaPlayer1.currentMedia;
            GetInfo(item);
        }
        #endregion
        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            curr = listBox1.SelectedIndex;
            IWMPMedia item = playlist.get_Item(curr);
            axWindowsMediaPlayer1.currentMedia = item;
            axWindowsMediaPlayer1.Ctlcontrols.play();
            GetInfo(item);
        }
    }
}
